package model

import (
	"database/sql"
	"encoding/json"
	"time"
	"errors"
)

type CreditCard struct {
	SecuritiesCompany
}

func redisCreditCard() (ret []*CreditCard, err error) {
	err = rdb.Del(ctx, "credit_card").Err()
	if err != nil {
		return ret, err
	}

	rows, err := db.Query("SELECT * FROM credit_card ORDER BY updated_date DESC")
	if err != nil {
		return ret, err
	}

	defer rows.Close()

	for rows.Next() {
		s := CreditCard{}
		if err := rows.Scan(&s.Id, &s.Name, &s.Img, &s.OfficialAdvantage, &s.ThickSnowAdvantage, &s.Addr, &s.TotalPromotionFee, &s.PromotionOfficerAward, &s.ApprenticeAward, &s.CreatedDate, &s.UpdatedDate, &s.Rules, &s.Status); err != nil {
			return ret, err
		}

		ret = append(ret, &s)

		jsonStu, err := json.Marshal(s)
		if err != nil {
			return ret, err
		}

		err = rdb.RPush(ctx, "credit_card", jsonStu).Err()
		if err != nil {
			return ret, err
		}
	}

	return ret, nil
}

func (s *CreditCard) GetCreditCard() (ret []*CreditCard, err error) {
	vals, err := rdb.LRange(ctx, "credit_card", 0, -1).Result()
	if err != nil {
		return nil, err
	}

	if len(vals) > 0 {
		for _, v := range vals {
			s := CreditCard{}
			err := json.Unmarshal([]byte(v), &s)
			if err != nil {
				return ret, err
			}

			ret = append(ret, &s)
		}

		return ret, nil
	}

	ret, err = redisCreditCard()
	if err != nil {
		return ret, err
	}

	return ret, nil
}

func (s *CreditCard) GetCreditCardDetails(id int) (ret CreditCard, err error) {
	stmt, err := db.Prepare("SELECT name, img, official_advantage, addr, rules FROM credit_card WHERE id=?")
	if err != nil {
		return ret, err
	}

	err = stmt.QueryRow(id).Scan(&ret.Name, &ret.Img, &ret.OfficialAdvantage, &ret.Addr, &ret.Rules)
	if err != nil && err != sql.ErrNoRows {
		return ret, err
	}

	if err == sql.ErrNoRows {
		return ret, errors.New("无效id")
	}

	return ret, nil
}

func (s *CreditCard) CreateCreditCard(se *CreditCard) error {
	stmt, err := db.Prepare("SELECT * FROM credit_card WHERE name=?")
	if err != nil {
		return err
	}

	err = stmt.QueryRow(se.Name).Scan(&s.Id)
	if err != nil && err != sql.ErrNoRows {
		return err
	}

	if err == sql.ErrNoRows {
		stmt1, err := db.Prepare("INSERT credit_card SET name=?, img=?, official_advantage=?, thick_snow_advantage=?, addr=?, total_promotion_fee=?, promotion_officer_award=?, apprentice_award=?, created_date=?, updated_date=?, rules=?, status=?")
		if err != nil {
			return err
		}

		_, err = stmt1.Exec(se.Name, se.Img, se.OfficialAdvantage, se.ThickSnowAdvantage, se.Addr, se.TotalPromotionFee, se.PromotionOfficerAward, se.ApprenticeAward, time.Now().Unix(), time.Now().Unix(), &se.Rules, 1)
		if err != nil {
			return err
		}

		_, err = redisCreditCard()
		if err != nil {
			return err
		}

		return nil
	}

	return errors.New("信用卡名称已存在")
}

func (s *CreditCard) UpdateCreditCard(se *CreditCard) error {
	stmt, err := db.Prepare("UPDATE credit_card SET name=?, img=?, official_advantage=?, thick_snow_advantage=?, addr=?, total_promotion_fee=?, promotion_officer_award=?, apprentice_award=?, updated_date=?, rules=? WHERE id=?")
	if err != nil {
		return err
	}

	_, err = stmt.Exec(se.Name, se.Img, se.OfficialAdvantage, se.ThickSnowAdvantage, se.Addr, se.TotalPromotionFee, se.PromotionOfficerAward, se.ApprenticeAward, time.Now().Unix(), se.Rules, se.Id)
	if err != nil {
		return err
	}

	defer stmt.Close()

	_, err = redisCreditCard()
	if err != nil {
		return err
	}

	return nil
}

func (s *CreditCard) DeleteCreditCard(status int, id int) error {
	stmt, err := db.Prepare("UPDATE credit_card SET status=? WHERE id=?")
	if err != nil {
		return err
	}

	_, err = stmt.Exec(status, id)
	if err != nil {
		return err
	}

	_, err = redisCreditCard()
	if err != nil {
		return err
	}

	return nil
}